<template>
    <div class="page">
        <el-row style="padding-bottom: 10px;">
            <el-col :span="6">
                <el-input placeholder="关键字搜索">
                    <el-button slot="append" icon="el-icon-search"></el-button>
                </el-input>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24" style="padding: 10px 0px 10px 0px;text-align: right;border-top: 1px solid #e4e7ed;">
                <el-button type="primary" icon="el-icon-edit-outline" @click="openAddDrawer">添加</el-button>
                <el-button type="primary" icon="el-icon-edit" @click="openUpdateDrawer"
                           :disabled="selectRows.length!==1">编辑
                </el-button>
                <el-button type="primary" icon="el-icon-info" @click="openUpdateDrawer"
                           :disabled="selectRows.length!==1">详情
                </el-button>
                <el-button type="danger" icon="el-icon-delete" @click="onDelete" :disabled="selectRows.length===0">删除
                </el-button>
            </el-col>
            <el-col :span="24" style="padding-bottom: 10px;">
                <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"/>
                    <el-table-column fixed prop="dictName" label="字典名称"></el-table-column>
                    <el-table-column prop="dictCode" label="字典标识"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间"></el-table-column>
                    <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                    <el-table-column
                        fixed="right"
                        label="操作"
                        width="100">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="text" size="small">字典项</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24" style="text-align: center;">
                <el-pagination background layout="total,prev, pager, next" :total="paginationCofig.total"
                               @size-change="sizeChange" :pageSize="paginationCofig.pageSize"
                               :currentPage.sync="paginationCofig.currentPage" @current-change="currentChange"
                               @prev-click="prevClick" @next-click="nextClick"></el-pagination>
            </el-col>
        </el-row>

        <!-- 新增 -->
        <el-drawer title="新增" :visible.sync="isAddDrawer" direction="rtl" :wrapperClosable="false"
                   :close-on-press-escape="false" :show-close="false">
            <el-row style="height: calc(100% - 42px);">
                <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                    <el-form-item label="字典名称" prop="dictName">
                        <el-input v-model="addForm.dictName" placeholder="字典名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="字典标识" prop="dictCode">
                        <el-input v-model="addForm.dictCode" placeholder="字典标识" style="width: 80%;">
                        </el-input>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row style="height: 42px;text-align: right;border-top: 1px solid #e4e7ed;padding-top: 10px;">
                <el-button size="small" type="primary" icon="el-icon-check" @click="addSave">保存</el-button>
                <el-button size="small" icon="el-icon-refresh-right" @click="resetForm('addForm')">重置</el-button>
                <el-button size="small" type="danger" icon="el-icon-close" @click="isAddDrawer=false">关闭</el-button>
            </el-row>
        </el-drawer>
        <!-- 编辑 -->
        <el-drawer title="编辑" :visible.sync="isUpdateDrawer" direction="rtl" :wrapperClosable="false"
                   :close-on-press-escape="false" :show-close="false">
            <el-row style="height: calc(100% - 42px);">
                <el-form :model="updateForm" :rules="rules" ref="updateForm" label-width="100px">
                    <el-form-item label="会员名称" prop="memberName">
                        <el-input v-model="updateForm.memberName" placeholder="会员名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="会员性别" prop="memberSex">
                        <el-select v-model="updateForm.memberSex" placeholder="会员性别" style="width: 80%;">
                            <el-option label="女" value="0"></el-option>
                            <el-option label="男" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row style="height: 42px;text-align: right;border-top: 1px solid #e4e7ed;padding-top: 10px;">
                <el-button size="small" type="primary" icon="el-icon-check" @click="updateSave">保存</el-button>
                <el-button size="small" icon="el-icon-refresh-right" @click="resetForm('updateForm')">重置</el-button>
                <el-button size="small" type="danger" icon="el-icon-close" @click="isUpdateDrawer=false">关闭</el-button>
            </el-row>
        </el-drawer>
        <!-- 详情 -->
        <el-drawer title="详情" :visible.sync="isDetailDrawer" direction="rtl">
            <span>我来啦!</span>
        </el-drawer>

    </div>
</template>

<script>
import _ from 'lodash'
import $api from '@/api/dict'

const formObj = {
    dictName: '',
    dictCode: '',
}

export default {
    data() {
        return {
            /* 分页tiao */
            paginationCofig: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            /* 表格data */
            tableData: [],
            selectRows: [],
            /* 抽屉 */
            isAddDrawer: false,
            isUpdateDrawer: false,
            isDetailDrawer: false,

            /* 表单 */
            addForm: null,
            updateForm: null,
            rules: {
                dictName: [
                    {required: true, message: '请输入字典名称', trigger: 'blur'},
                    {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                ],
                dictCode: [
                    {required: true, message: '请输入字典标识（大写英文字母）', trigger: 'change'}
                ],
            }
        };
    },
    methods: {
        handleSelectionChange(val) {
            this.selectRows = val
        },
        onDelete() {
            const deleteIds = this.selectRows.map(item => {
                return item.memberNo
            }).join(",")
            $api.deleteMembers(deleteIds).then(data => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.pageList()
            })
        },
        /* 新增抽屉 */
        openAddDrawer() {
            this.isAddDrawer = true
            this.resetForm("addForm")
        },
        addSave() {
            this.validateForm("addForm", (success, errorObj) => {
                if (success) {
                    const formData = this.addForm
                    // console.log(formData)
                    $api.addDict(formData).then(data => {
                        this.$message({
                            message: '新增成功',
                            type: 'success'
                        });
                        this.isAddDrawer = false
                        this.pageList()
                    })
                }
            })
        },
        /* 修改抽屉 */
        openUpdateDrawer() {
            this.isUpdateDrawer = true
        },
        updateSave() {
            this.validateForm("updateForm", (success, errorObj) => {
                if (success) {
                    const memberNo = this.selectRows[0].memberNo
                    const formData = this.updateForm
                    $api.updateMember(memberNo, formData).then(data => {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        this.isUpdateDrawer = false
                        this.pageList()
                    })
                }
            })
        },
        /* 详情抽屉 */
        openDetailDrawer() {
            this.isDetailDrawer = true
        },

        /* 分页条-start */
        sizeChange(pageSize) {
            this.pageList()
        },
        currentChange(currentPage) {
            this.pageList()
        },
        prevClick(pageSize) {
            this.pageList()
        },
        nextClick(currentPage) {
            this.pageList()
        },
        /*表格操作列*/
        handleClick(row){
            // console.log(row)
            const dictId = row.id
            this.$router.push({path:'/dict/dictItemMg',query:{dictId}})
        },
        /* api */
        pageList(query) {
            const currentPage = this.paginationCofig.currentPage
            const pageSize = this.paginationCofig.pageSize
            $api.dictListPage(currentPage, pageSize, query).then(data => {
                // console.log(data.data);
                this.paginationCofig.total = data.data.total

                this.tableData = data.data.records
            })

        },

        /* common methods */
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        validateForm(formName, callback) {
            this.$refs[formName].validate(callback);
        },

    },
    mounted() {
        this.pageList()
    },
    created() {
        this.addForm = {}
        this.updateForm = {}
        this.addForm = _.cloneDeep(formObj)
        this.updateForm = _.cloneDeep(formObj)
    }
};
</script>

<style>
/*.page {
    padding: 10px;
}*/
</style>